package org.openstreetmap.gui.jmapviewer;

import org.openstreetmap.gui.jmapviewer.interfaces.MapMarker;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.util.ArrayList;

public class MapLocationOld {

    double lat;
    double lon;
	double minLat, maxLat;
	double minLon, maxLon;
	int time;
	int quantity;
	int radius = 20;
	String label;
	String text;
	Font font = new Font("Sans Serif", Font.BOLD, 12);
    Color colorBackground, borderColor = new Color(0, 0, 0, 100);

	private ArrayList<MapMarker> mapMarkers = new ArrayList<>();
	private int mapLocationID;

    public MapLocationOld(double lat, double lon, int quantity, String label) {
        this(new Color(0, 255, 0, 150), lat, lon, quantity, label);
    }

    public MapLocationOld(Color colorBackground, double lat, double lon, int quantity, String label) {
        this.colorBackground = colorBackground;
        this.lat = lat;
        this.lon = lon;
	    this.quantity = quantity;
	    this.label = label;
    }

	public MapLocationOld(double lat, double lon, double minLat, double minLon, double maxLat, double maxLon,
	                      int quantity, String label, int time) {
		this(new Color(0, 255, 0, 150), lat, lon, minLat, minLon, maxLat, maxLon, quantity, label);
		this.time = time;
	}

	public MapLocationOld(double lat, double lon, double minLat, double minLon, double maxLat, double maxLon,
	                      int quantity, String label) {
		this(new Color(0, 255, 0, 150), lat, lon, minLat, minLon, maxLat, maxLon, quantity, label);
	}

	public MapLocationOld(Color colorBackground, double lat, double lon, double minLat, double minLon, double maxLat, double maxLon,
	                      int quantity, String label) {
		this.lat = lat;
		this.lon = lon;
		this.minLat = minLat;
		this.maxLat = maxLat;
		this.minLon = minLon;
		this.maxLon = maxLon;
		this.quantity = quantity;
		this.radius = getRadius(quantity);
		this.label = label;
		this.colorBackground = colorBackground;
	}


	public double getLat() {
        return lat;
    }

    public double getLon() {
        return lon;
    }

	public double getMinLat() {
		return minLat;
	}

	public double getMaxLat() {
		return maxLat;
	}

	public double getMinLon() {
		return minLon;
	}

	public double getMaxLon() {
		return maxLon;
	}

	public int getQuantity() {
		return quantity;
	}

	public int getTime() {
		return time;
	}

	public int getMapLocationID() {
		return mapLocationID;
	}

	public void setMapLocationID(int mapLocationID) {
		this.mapLocationID = mapLocationID;
	}

	public void addMapMarkers(ArrayList<MapMarker> mapMarkers) {
		for (MapMarker mapMarker : mapMarkers) {
			mapMarker.setIsInLocation(true);
			mapMarker.setMapLocationID(mapLocationID);
			this.mapMarkers.add(mapMarker);
		}
//		this.mapMarkers.addAll(mapMarkers);
	}

	public int getRadius(int x, int y, int minX, int minY, int maxX, int maxY) {
		int a = x - minX;
		int b = y - maxY;
		int hyp1 = (int) Math.sqrt(a * a + b * b);
		a = maxX - x;
		b = minY - y;
		int hyp2 = (int) Math.sqrt(a * a + b * b);

		if (hyp1 > hyp2) return hyp1;
		return hyp2;
	}

	private int getRadius(int quantity) {
		if (quantity > 10) quantity = 10;
		return 9 + quantity * 2;
	}

	public void paint(Graphics g, Point position, Point position1, Point position2, int zoom) {
		int radius = zoom > 15 ?
				getRadius(position.x, position.y, position1.x, position2.y, position2.x, position1.y) : this.radius;
		int diameter = radius * 2;

		Graphics2D g2 = (Graphics2D) g;
		g2.setRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON));
		g2.setColor(colorBackground);

        g2.fillOval(position.x - radius, position.y - radius, diameter, diameter);

        g2.setColor(borderColor);
		g2.setStroke(new BasicStroke(2));

		g2.drawOval(position.x - radius, position.y - radius, diameter, diameter);


		g2.setFont(font);
		FontMetrics metrics = g2.getFontMetrics();
		text = label/* + " " + mapLocationID*/;
		int textWidth = metrics.stringWidth(text);
		int textHeight = metrics.getHeight();
		int x = position.x - textWidth / 2 + 1;
		int y = position.y + textHeight / 2 - 2;

		g2.setColor(Color.WHITE);
		g2.drawString(text, ShiftWest(x, 1), ShiftNorth(y, 1));
		g2.drawString(text, ShiftWest(x, 1), ShiftSouth(y, 1));
		g2.drawString(text, ShiftEast(x, 1), ShiftNorth(y, 1));
		g2.drawString(text, ShiftEast(x, 1), ShiftSouth(y, 1));
		g2.setColor(Color.BLACK);
		g2.drawString(text, x, y);
	}

	int ShiftNorth(int p, int distance) {
		return (p - distance);
	}

	int ShiftSouth(int p, int distance) {
		return (p + distance);
	}

	int ShiftEast(int p, int distance) {
		return (p + distance);
	}

	int ShiftWest(int p, int distance) {
		return (p - distance);
	}
}

